package com.jf.cloud.service.sysmanager.support.entity;

import java.io.Serializable;

import com.google.gson.Gson;
import com.jf.cloud.service.sysmanager.constant.Constant;

/**
 * Created with IntelliJ IDEA.
 * User: LangK
 * Date: 2017/11/20
 * Time: 11:16
 * Description:接口返回的基础类
 */
public class Result<T> implements Serializable {

    public static Gson gson = new Gson();

    /**
     * 结果码
     */
    private int code;
    /**
     * 返回备注信息
     */
    private String message;
    /**
     * 返回的数据体
     */
    private T data;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public  boolean ifSuccess(){
        if(code == 0){
            return true;
        }
        return false;
    }

    public static class ResultBuiler<T> {

        private Result result;

        public ResultBuiler() {
            this.result = new Result();
        }


        public ResultBuiler code(int code) {
            result.setCode(code);
            return this;
        }

        public ResultBuiler message(String message) {
            result.setMessage(message);
            return this;
        }

        public ResultBuiler data(T data) {
            result.setData(data);
            return this;
        }

        public Result failed(String message) {
            this.result.setMessage(message);
            this.result.setCode(Constant.RUSULT_FAILED);
            return this.result;
        }

        public Result failed(int code,String message) {
            this.result.setMessage(message);
            this.result.setCode(code);
            return this.result;
        }

        public Result error(String message) {
            this.result.setMessage(message);
            this.result.setCode(Constant.RUSULT_ERROR);
            return this.result;
        }

        public Result error() {
            if (this.result.getMessage() == null) {
                this.result.setMessage(Constant.RUSULT_ERROR_MES);
            }
            this.result.setCode(Constant.RUSULT_ERROR);
            return this.result;
        }

        public Result success(T t) {
            if (this.result.getMessage() == null) {
                this.result.setMessage(Constant.RUSULT_SUCCESS_MES);
            }
            this.result.setData(t);
            this.result.setCode(Constant.RUSULT_SUCCESS);
            return this.result;
        }

        public Result success() {
            if (this.result.getMessage() == null) {
                this.result.setMessage(Constant.RUSULT_SUCCESS_MES);
            }
            this.result.setCode(Constant.RUSULT_SUCCESS);
            return this.result;
        }

        public Result build() {
            return result;
        }
    }


    @Override
    public String toString() {
        return gson.toJson(this);
    }
}
